#!/usr/bin/env bash

####################### FUNCTIONS #########################

strand() {
	if [ $1 == A ];then
		all=T
	elif [ $1 == T ];then
		all=A
	elif [ $1 == C ];then
		all=G
	else  
		all=C
	fi
	echo $all
}

############### 


	echo "chrpos	IDdata	freqdba0	freqdataa0	deltafreq" > "output_liftoversh/keepchrpos_"$name"_"$2".txt"
	echo "chrpos	IDdata	freqdba0	freqdataa0	deltafreq" > "output_liftoversh/outchrpos_"$name"_"$2".txt"
	th=`echo "scale=1;(1/10)"|bc`
	while read line
	do   
		while IFS="	" read chrpos IDdata chr IDdb a0db a1db freqdba0 freqdataa0 deltafreq a0data a1data
			do
				if [ $a0db == $a0data ] && [ $a1db == $a1data ];then
					if test $deltafreq \< $th ;then 
						echo "$chrpos	$IDdata	$freqdba0	$freqdataa0	$deltafreq"  >>"output_liftoversh/keepchrpos_"$name"_"$2".txt"
					else
						echo "$chrpos	$IDdata	$freqdba0	$freqdataa0	$deltafreq" >> "output_liftoversh/outchrpos_"$name"_"$2".txt"
					fi
				elif [ $a0db == $(strand $a0data) ] && [ $a1db == $(strand $a1data) ];then
					if test $deltafreq \< $th ;then 
						echo "$chrpos	$IDdata	$freqdba0	$freqdataa0	$deltafreq"  >>"output_liftoversh/keepchrpos_"$name"_"$2".txt"
					else
						echo "$chrpos	$IDdata	$freqdba0	$freqdataa0	$deltafreq" >> "output_liftoversh/outchrpos_"$name"_"$2".txt"
					fi
				elif [ $a0db == $a1data ] && [ $a1db == $a0data ];then
					newfreqdba0=`echo "scale=2;1-$freqdba0" |bc`
					delta=`echo "scale=2;$newfreqdba0 - $freqdataa0" |bc` 
					newdelta=${delta#-} #absolute value
					if test $newdelta \< $th ;then
						echo " passe 3keep : $chrpos	$IDdata	$freqdba0	$freqdataa0	$deltafreq $a0db $a1db $a0data $a1data" >> "verbose.txt"
						echo "$chrpos	$IDdata	$newfreqdba0	$freqdataa0	$newdelta"  >> "output_liftoversh/keepchrpos_"$name"_"$2".txt"
					else
						echo " passe 3out : $chrpos	$IDdata	$freqdba0	$freqdataa0	$deltafreq $a0db $a1db $a0data $a1data" >> "verbose.txt"
						echo "$chrpos	$IDdata	$newfreqdba0	$freqdataa0	$newdelta">> "output_liftoversh/outchrpos_"$name"_"$2".txt"
					fi
				elif [ $a0db == $(strand $a1data) ] && [ $a1db == $(strand $a0data) ];then
					newfreqdba0=`echo "scale=2;1-$freqdba0" |bc`
					delta=`echo "scale=2;$newfreqdba0 - $freqdataa0" |bc` 
					newdelta=${delta#-} #absolute value
					if test $newdelta \< $th ;then
						echo " passe 4keep : $chrpos	$IDdata	$freqdba0	$freqdataa0	$deltafreq $a0db $a1db strand $a0data strand $a1data" >> "verbose.txt"
						echo "$chrpos	$IDdata	$newfreqdba0	$freqdataa0	$newdelta"  >> "output_liftoversh/keepchrpos_"$name"_"$2".txt"
					else
						echo " passe 4out : $chrpos	$IDdata	$freqdba0	$freqdataa0	$deltafreq $a0db $a1db strand $a0data strand $a1data" >> "verbose.txt"
						echo "$chrpos	$IDdata	$newfreqdba0	$freqdataa0	$newdelta">> "output_liftoversh/outchrpos_"$name"_"$2".txt"
					fi
				else
					echo  "$chrpos	$IDdata	$freqdba0	$freqdataa0	$deltafreq" >> "output_liftoversh/outchrpos_"$name"_"$2".txt"
				fi
			done
	done < $1
	echo -e "see list of SNPs to keep : output_liftoversh/keepchrpos_"$name"_"$2".txt" 
	echo -e "see list of SNPs to del : output_liftoversh/outchrpos_"$name"_"$2".txt" 

